package com.yalu.service.data.impl;

import java.util.List;

import javax.annotation.Resource;

import com.sun.org.apache.commons.beanutils.BeanUtils;
import com.yalu.dao.data.SalesDao;
import com.yalu.entity.base.BaseEntity;
import com.yalu.entity.data.Sales;
import com.yalu.entity.data.SalesKey;
import com.yalu.service.base.impl.BaseServiceImpl;
import com.yalu.service.data.SalesService;
import com.yalu.util.BaseUtil;

@Service
public class SalesServiceImpl extends BaseServiceImpl<Sales, SalesKey>
implements SalesService {

	@Resource
	private SalesDao salesDao;

	@Resource
	public void setBaseDao(SalesDao salesDao) {
		super.setBaseDao(salesDao);
	}

	@Override
	public int[] save(List<BaseEntity> entityList) {
		int[] returnCounts = new int[2];
		int saveCount = 0;
		int updateCount = 0;
		String time = BaseUtil.getTime();
		for(BaseEntity baseEntity : entityList){
			Sales entity = (Sales)baseEntity;
			SalesKey key  = entity.getSalesKey();
			Sales temp = salesDao.get(key);
			if(temp==null){
				entity.setCreatedt(time);
				salesDao.save(entity);
				saveCount++;
			}else{
				entity.setUpdatedt(time);
				BeanUtils.copyProperties(entity,temp, new String[] { "no", "isdeleted", "createdt",
						"createby", "updateby" });
				salesDao.update(temp);
				updateCount++;
			}
		}

		returnCounts[0] = saveCount;
		returnCounts[1] = updateCount;
		return returnCounts;
	}
	
	@Override
	public List<Sales> getDataListByTime(String beginTime, String endTime) {
		List<Sales> datalist = salesDao.getDataListByTime(beginTime, endTime);
		return datalist;
	}
}
